/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.controle;

import br.com.ecommercer.bean.Endereco;
import br.com.ecommercer.bean.Evento;
import br.com.ecommercer.dao.EnderecoDao;
import br.com.ecommercer.dao.EnderecoDaoImp;
import br.com.ecommercer.dao.EventoDao;
import br.com.ecommercer.dao.EventoDaoImp;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Petterson
 */
@WebServlet(name = "EventoServlet", urlPatterns = {"/EventoServlet"})
public class EventoServlet extends HttpServlet {

    private HttpServletRequest request;
    private HttpServletResponse response;
    private RequestDispatcher rd;
    private Evento evento;
    private EventoDao eventoDao;

    protected void processRequest() throws ServletException, IOException {
        String cmd = request.getParameter("cmd");
        switch (cmd) {
            case "salvar":
                salvar();
                break;
            case "alterar":
                //alterar();
                break;
            case "excluir":
                excluir();
                break;
            case "procurarId":
                procurarPorId();
                break;
            case "pesqNome":
                procuraPorNome();
                break;
            case "cadEvento":
                carregaCombo();
                break;
        }
        rd.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Date converteData(String dataString) {
        Date data = null;
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        try {
            data = format.parse(dataString);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return data;
    }

    private void salvar() {
        carregaEvento();
        eventoDao = new EventoDaoImp();
        try {
            eventoDao.salvar(evento);
            request.setAttribute("msg", "Evento salvo com sucesso!");
            rd = request.getRequestDispatcher("index.jsp");
        } catch (Exception e) {
            System.out.println("erro " + e.getMessage());
        }
    }

    private void carregaEvento() {
        evento = new Evento();
        String id = request.getParameter("id");
        if (id != null) {
            evento.setId(Integer.parseInt(id));
        }
        evento.setNome("nome");
        evento.setArtista("artista");
        evento.setEstilo("estilo");
        evento.setDescricao("descricao");
        //converter a data
        String converterData = request.getParameter("dataEvento");
        Date data = converteData(converterData);
        evento.setDataEvento(data);
        evento.setNome("totalIngresso");
        evento.setNome("endereco");

        Endereco endereco = new Endereco();
        String idEnd = request.getParameter("idEndereco");
        endereco.setId(Integer.parseInt(idEnd));
        //Integer idEnde = Integer.parseInt(idEndeConv);
        evento.setEndereco(endereco);
    }

    private void procuraPorNome() {
        evento = new Evento();
        eventoDao = new EventoDaoImp();
        try {
            List eventos = eventoDao.pesquisaNome(request.getParameter("nome"));
            if (eventos.isEmpty()) {
                request.setAttribute("msg", "Não existe evento");
            }
            request.setAttribute("eventos", eventos);
            rd = request.getRequestDispatcher("pesq_evento.jsp");
        } catch (Exception e) {
            System.out.println(" " + e.getMessage());
        }
    }

    private void procurarPorId() {
        eventoDao = new EventoDaoImp();
        Integer id = Integer.parseInt(request.getParameter("id"));
        try {
            //Object evento = eventoDao.procurar(id);
            //request.setAttribute("evento", evento);
        } catch (Exception e) {
        }
    }

    private void excluir() {
        eventoDao = new EventoDaoImp();
        Integer id = Integer.parseInt(request.getParameter("id"));
        try {
            eventoDao.excluir(id);
            request.setAttribute("msg", "Excluido com Sucesso!");
        } catch (Exception e) {
            request.setAttribute("msg", "Erro ao excluido evento!");
        }
        rd = request.getRequestDispatcher("pesq_evento.jsp");
    }

    private void carregaCombo() {
        EnderecoDao enderecoDao = new EnderecoDaoImp();
        try {
            List enderecos = enderecoDao.listar();
            request.setAttribute("enderecos", enderecos);
            rd = request.getRequestDispatcher("cad_evento.jsp");
        } catch (Exception e) {
            System.out.println("erro ao carregar combo " + e.getMessage());
        }
    }
}
